﻿/*
BF4Rcon.NET, provides a .NET implementation of BF4's RCON interface.
Copyright (C) 2013 Tim Marro

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using TimScripts.BF4Rcon.Frostbite;

namespace TimScripts.BF4Rcon.Test
{
    [TestFixture]
    public class PlayerCollectionTests
    {
        PlayerRconClient Client;
        PlayerCollection Players;

        [SetUp]
        public void SetUp()
        {
            Client = new PlayerRconClient();
            Players = new PlayerCollection(Client);
        }

        [Test]
        public void EnumeratesPlayers()
        {
            int i = 0;
            string temp;
            string[] playerNames = { "player1", "player2" };

            Client.Players.AddRange(playerNames);

            foreach (Player p in Players)
            {
                Assert.AreEqual(playerNames[i++], p.Name);

                //dont care about value, just presence
                Assert.AreEqual(true, p.TryGetProperty("awesomeness", out temp));
            }
        }

        [Test]
        public void EnumeratesZeroPlayers()
        {
            //use linq one for enumerator
            Assert.AreEqual(0, Players.Count());
        }

        [Test]
        public void CountsCorrectNumberOfPlayers()
        {
            Client.Players.AddRange(new[] { "", "", "" });
            Assert.AreEqual(3, Players.Count);
        }

        [Test]
        public void IndexerGetsCorrectPlayers()
        {
            Client.Players.AddRange(new[] { "0", "1", "2" });

            var result = Players.Select((p, i) => new { Actual = int.Parse(p.Name), Expected = i });

            foreach (var r in result)
            {
                Assert.AreEqual(r.Expected, r.Actual);
            }
        }

        [Test]
        public void IndexerThrowsArgOutOfRange()
        {
            Assert.Throws<ArgumentOutOfRangeException>(() => { var x = Players[0]; });
        }


    }
}
